﻿using HDM.NexusEngine.Core;
using HDM.NexusEngine.Input;
using HDM.NexusEngine.PathFinding;

namespace HDM.NexusEngine.Editor
{
    public class AddVertexState : IGameObject
    {
        private readonly InputManager _input;
        private readonly NavMesh _navMesh;
        private ConvexPolygon _selectedPolygon;

        public AddVertexState(InputManager input, NavMesh navMesh)
        {
            _input = input;
            _navMesh = navMesh;
        }

        public void Update(double elapsedTime)
        {
            _selectedPolygon = _navMesh.FindNearestPolygon(_input.Mouse.Position);
            if (_input.Mouse.LeftPressed)
            {
                if (_selectedPolygon != null)
                {
                    _selectedPolygon.TryToAddVertex(_input.Mouse.Position);
                }
            }
        }

        public void Render()
        {
            RenderEditableVertices();
        }

        public void Activated()
        {

        }

        private void RenderEditableVertices()
        {
            if (_selectedPolygon == null) return;

            foreach (var t in _selectedPolygon.Vertices)
            {
                GLUtil.DrawCircle(t, 15, new Color(0, 0, 0, 1));
            }
        }
    }
}